
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Testing GeoDjango apps &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="部署 GeoDjango" href="deployment.html" />
    <link rel="prev" title="Geographic Sitemaps" href="sitemaps.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="sitemaps.html" title="Geographic Sitemaps">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="deployment.html" title="部署 GeoDjango">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-gis-testing">
            
  <div class="section" id="s-testing-geodjango-apps">
<span id="testing-geodjango-apps"></span><h1>Testing GeoDjango apps<a class="headerlink" href="#testing-geodjango-apps" title="永久链接至标题">¶</a></h1>
<p>Included in this documentation are some additional notes and settings
for <a class="reference internal" href="#testing-postgis"><span class="std std-ref">PostGIS</span></a> users.</p>
<div class="section" id="s-postgis">
<span id="s-testing-postgis"></span><span id="postgis"></span><span id="testing-postgis"></span><h2>PostGIS<a class="headerlink" href="#postgis" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-settings">
<span id="settings"></span><h3>配置<a class="headerlink" href="#settings" title="永久链接至标题">¶</a></h3>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">The settings below have sensible defaults, and shouldn't require manual setting.</p>
</div>
<div class="section" id="s-postgis-version">
<span id="s-std:setting-POSTGIS_VERSION"></span><span id="postgis-version"></span><span id="std:setting-POSTGIS_VERSION"></span><h4><code class="docutils literal notranslate"><span class="pre">POSTGIS_VERSION</span></code><a class="headerlink" href="#postgis-version" title="永久链接至标题">¶</a></h4>
<p>When GeoDjango's spatial backend initializes on PostGIS, it has to perform
an SQL query to determine the version in order to figure out what
features are available. Advanced users wishing to prevent this additional
query may set the version manually using a 3-tuple of integers specifying
the major, minor, and micro version numbers for PostGIS. For example,
to configure for PostGIS X.Y.Z you would use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">POSTGIS_VERSION</span> <span class="o">=</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">,</span> <span class="n">Z</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-obtaining-sufficient-privileges">
<span id="obtaining-sufficient-privileges"></span><h3>Obtaining sufficient privileges<a class="headerlink" href="#obtaining-sufficient-privileges" title="永久链接至标题">¶</a></h3>
<p>Depending on your configuration, this section describes several methods to
configure a database user with sufficient privileges to run tests for
GeoDjango applications on PostgreSQL. If your
<a class="reference internal" href="install/postgis.html#spatialdb-template"><span class="std std-ref">spatial database template</span></a>
was created like in the instructions, then your testing database user
only needs to have the ability to create databases. In other configurations,
you may be required to use a database superuser.</p>
<div class="section" id="s-create-database-user">
<span id="create-database-user"></span><h4>创建数据库普通用户<a class="headerlink" href="#create-database-user" title="永久链接至标题">¶</a></h4>
<p>To make a database user with the ability to create databases, use the
following command:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ createuser --createdb -R -S &lt;user_name&gt;
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">-R</span> <span class="pre">-S</span></code> flags indicate that we do not want the user to have the ability
to create additional users (roles) or to be a superuser, respectively.</p>
<p>Alternatively, you may alter an existing user's role from the SQL shell
(assuming this is done from an existing superuser account):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">postgres</span><span class="c1"># ALTER ROLE &lt;user_name&gt; CREATEDB NOSUPERUSER NOCREATEROLE;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-create-database-superuser">
<span id="create-database-superuser"></span><h4>创建数据库超级用户<a class="headerlink" href="#create-database-superuser" title="永久链接至标题">¶</a></h4>
<p>This may be done at the time the user is created, for example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ createuser --superuser &lt;user_name&gt;
</pre></div>
</div>
<p>Or you may alter the user's role from the SQL shell (assuming this
is done from an existing superuser account):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">postgres</span><span class="c1"># ALTER ROLE &lt;user_name&gt; SUPERUSER;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-windows">
<span id="windows"></span><h3>Windows<a class="headerlink" href="#windows" title="永久链接至标题">¶</a></h3>
<p>On Windows platforms you can use the pgAdmin III utility to add superuser
privileges to your database user.</p>
<p>By default, the PostGIS installer on Windows includes a template
spatial database entitled <code class="docutils literal notranslate"><span class="pre">template_postgis</span></code>.</p>
</div>
</div>
<div class="section" id="s-geodjango-tests">
<span id="s-id1"></span><span id="geodjango-tests"></span><span id="id1"></span><h2>GeoDjango tests<a class="headerlink" href="#geodjango-tests" title="永久链接至标题">¶</a></h2>
<p>To have the GeoDjango tests executed when <a class="reference internal" href="../../../internals/contributing/writing-code/unit-tests.html#running-unit-tests"><span class="std std-ref">running the Django test suite</span></a> with <code class="docutils literal notranslate"><span class="pre">runtests.py</span></code> all of the databases in the settings
file must be using one of the <a class="reference internal" href="db-api.html#spatial-backends"><span class="std std-ref">spatial database backends</span></a>.</p>
<div class="section" id="s-example">
<span id="example"></span><h3>例如<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h3>
<p>The following is an example bare-bones settings file with spatial backends
that can be used to run the entire Django test suite, including those
in <a class="reference internal" href="index.html#module-django.contrib.gis" title="django.contrib.gis: Geographic Information System (GIS) extensions for Django"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.gis</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">DATABASES</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;default&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s1">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s1">&#39;django.contrib.gis.db.backends.postgis&#39;</span><span class="p">,</span>
        <span class="s1">&#39;NAME&#39;</span><span class="p">:</span> <span class="s1">&#39;geodjango&#39;</span><span class="p">,</span>
        <span class="s1">&#39;USER&#39;</span><span class="p">:</span> <span class="s1">&#39;geodjango&#39;</span><span class="p">,</span>
    <span class="p">},</span>
    <span class="s1">&#39;other&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s1">&#39;ENGINE&#39;</span><span class="p">:</span> <span class="s1">&#39;django.contrib.gis.db.backends.postgis&#39;</span><span class="p">,</span>
        <span class="s1">&#39;NAME&#39;</span><span class="p">:</span> <span class="s1">&#39;other&#39;</span><span class="p">,</span>
        <span class="s1">&#39;USER&#39;</span><span class="p">:</span> <span class="s1">&#39;geodjango&#39;</span><span class="p">,</span>
    <span class="p">},</span>
<span class="p">}</span>

<span class="n">SECRET_KEY</span> <span class="o">=</span> <span class="s1">&#39;django_tests_secret_key&#39;</span>
</pre></div>
</div>
<p>Assuming the settings above were in a <code class="docutils literal notranslate"><span class="pre">postgis.py</span></code> file in the same
directory as <code class="docutils literal notranslate"><span class="pre">runtests.py</span></code>, then all Django and GeoDjango tests would
be performed when executing the command:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ ./runtests.py --settings=postgis
</pre></div>
</div>
<p>To run only the GeoDjango test suite, specify <code class="docutils literal notranslate"><span class="pre">gis_tests</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ ./runtests.py --settings=postgis gis_tests
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Testing GeoDjango apps</a><ul>
<li><a class="reference internal" href="#postgis">PostGIS</a><ul>
<li><a class="reference internal" href="#settings">配置</a><ul>
<li><a class="reference internal" href="#postgis-version"><code class="docutils literal notranslate"><span class="pre">POSTGIS_VERSION</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#obtaining-sufficient-privileges">Obtaining sufficient privileges</a><ul>
<li><a class="reference internal" href="#create-database-user">创建数据库普通用户</a></li>
<li><a class="reference internal" href="#create-database-superuser">创建数据库超级用户</a></li>
</ul>
</li>
<li><a class="reference internal" href="#windows">Windows</a></li>
</ul>
</li>
<li><a class="reference internal" href="#geodjango-tests">GeoDjango tests</a><ul>
<li><a class="reference internal" href="#example">例如</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="sitemaps.html"
                        title="上一章">Geographic Sitemaps</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="deployment.html"
                        title="下一章">部署 GeoDjango</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/gis/testing.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="sitemaps.html" title="Geographic Sitemaps">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="deployment.html" title="部署 GeoDjango">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>